'''
Created on 12/01/2013

@author: Jorge
'''

from Classifier import Classifier
import numpy as np
from sklearn.naive_bayes import GaussianNB
from sklearn import svm
import sklearn
from dataset.DatasetImplementations import DatasetForLibSVM
from classifiers.svm_tree import SVMTree
import mlpy
from SVMTreeExample import SVMTreeExample
from svm_tree import SVMTree
from feature_selection.SVMTreeFeatureSelection import *
from sklearn import tree

class NaiveBayes(Classifier):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''

        
    def train(self, train_set):
        
        X,Y,upper_Y = self._get_X_Y_upper_Y_vectors(train_set)
        
        x = np.array(X)
        y = np.array(upper_Y)
        self.upper_svm = svm.SVC(kernel='rbf')
        #self.upper_svm = GaussianNB()
        self.upper_svm.fit(x, y)
        
        self.minority_model = svm.SVC(kernel='linear', C=16, gamma=0.03125)
        minority_X, minority_Y= self.__get_data_by_tag(X,Y, upper_Y, SVMTree.MINOTIRY_TAG)
        minority_X = self._apply_feature_selection(minority_X, MinoritySVMFeatures())
        self.minority_model.fit(minority_X, minority_Y)
        
        self.majority_model = svm.SVC(kernel='linear', C=16, gamma=0.007812)
        majority_X, majority_Y= self.__get_data_by_tag(X,Y, upper_Y, SVMTree.MAJORITY_TAG)
        majority_X = self._apply_feature_selection(majority_X, MajoritySVMFeatures())
        self.majority_model.fit(majority_X, majority_Y)
        
    def test(self, test_set):
        
        X,Y,upper_Y = self._get_X_Y_upper_Y_vectors(test_set)

        x = np.array(X)
        y = np.array(upper_Y)
        p_labels  = self.upper_svm.predict(x)
        
        print sklearn.metrics.f1_score(y, p_labels)
        print sklearn.metrics.confusion_matrix(y, p_labels)
        
        minority_X, minority_Y, majority_X, majority_Y = [], [], [], []
        for i in range(len(p_labels)):
            print 'p_label', p_labels[i]
            if  upper_Y[i] == SVMTreeExample.get_number_upper_label(SVMTree.MINOTIRY_TAG):
                minority_X.append(X[i])
                minority_Y.append(Y[i])
            else:
                majority_X.append(X[i])
                majority_Y.append(Y[i])
        
        minority_X = self._apply_feature_selection(minority_X, MinoritySVMFeatures())
        majority_X = self._apply_feature_selection(majority_X, MajoritySVMFeatures())
        
        minority_labels = self.minority_model.predict(minority_X)
        print 'minority model f1: ', sklearn.metrics.f1_score(minority_Y, minority_labels)
        print sklearn.metrics.confusion_matrix(minority_Y, minority_labels)
        
        majority_labels = self.majority_model.predict(majority_X)
        print 'majority model f1: ', sklearn.metrics.f1_score(majority_Y, majority_labels)
        print sklearn.metrics.confusion_matrix(majority_Y, majority_labels)
        


    def jaja(self, v):
        output=[]
        for i in v:
            if i==0:
                output.append(-1)
            else:
                output.append(1)
        return output
    
    def __get_data_by_tag(self, X, Y, tags, tag):
        output_x =[]
        output_y=[]
        for i in range(len(tags)):
            if tags[i] == SVMTreeExample.get_number_upper_label(tag):
                output_x.append(X[i])
                output_y.append(Y[i])
        return output_x, output_y
       
        

if __name__ == '__main__':
    classifier = NaiveBayes()
    
    data = DatasetForLibSVM()
    training_set = data.get_training_set()
    training_set.extend(data.get_validation_set())
    test_set = data.get_test_set()
    
    training_set, test_set =  SVMTree.convert_example(training_set, test_set)
    
    classifier.train(training_set)
    classifier.test(test_set)